package com.my.biz.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.my.biz.dao.TokenMapper;
import com.my.biz.domain.TokenDO;
import com.my.biz.service.ITokenService;
import com.my.core.base.dao.GenericDao;
import com.my.core.base.service.AbstractGenericService;
import com.my.util.JWTUtil;


 /**
 * 
 * @描述：ITokenService 服务实现类
 * 
 * @创建人： ch
 * 
 * @创建时间：2017年07月04日 21:10:39
 * 
 */
@Service("iTokenService")
public class TokenServiceImpl extends AbstractGenericService<TokenDO, Long> implements ITokenService{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TokenServiceImpl.class);
	
	private static final Long OUT_TIME = 86400000l*3;
	
	@Autowired
	private TokenMapper tokenMapper;
	
	@Autowired
	private JWTUtil jwtUtil;
	
	@Override
    public GenericDao<TokenDO, Long> getDao() {
        return tokenMapper;
    }

	@Transactional(rollbackFor=Exception.class)
	@Override
	public String creatToken(String uid) {
		Long expireTime = System.currentTimeMillis()+OUT_TIME;
		String token = jwtUtil.creatToken(uid, expireTime);
		TokenDO c = new TokenDO(uid);
		c=selectOne(c);
		if(c == null){
			insert(new TokenDO(uid,token,expireTime));
		}else{
			update(new TokenDO(token,expireTime), new TokenDO(c.getUid()));
		}
		return token;
	}
    


}
